package com.liferay.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.liferay.contant.Contants;
import com.liferay.dao.ConnectDao;
import com.liferay.dao.ContactDao;
import com.liferay.model.Contact;

public class ContactDaoImp implements ContactDao {
	private Connection connection;
	private final String GET_ALL_CONTACT = "SELECT * FROM nlu_Contact";
	private final String SAVE_CONTACT = "INSERT INTO nlu_Contact (fullName,email,phone,title,question,dateCreate,isRead) VALUES(?,?,?,?,?,?,?);";
	private final String GET_CONTACT_BYID = "SELECT * FROM nlu_Contact WHERE id=?;";
	private final String UPDATE_CONTACT_ISREAD = "UPDATE nlu_Contact SET isRead=1 WHERE id=?;";
	
	
	public ContactDaoImp() {
		connection = ConnectDao.getConnection();
	}

	public List<Contact> getAllContact(long pageIndex, long pageSize) {
		List<Contact> list = new ArrayList<Contact>();
		try {
			PreparedStatement preSta = connection
					.prepareStatement(GET_ALL_CONTACT);
			//preSta.setLong(1, pageIndex);
			//preSta.setLong(2, pageSize);
			ResultSet rs = preSta.executeQuery();

			while (rs.next()) {
				Contact contact = new Contact();
				contact.setContactID(rs.getLong("id"));
				contact.setFullName(rs.getString("fullName"));
				contact.setEmail(rs.getString("email"));
				contact.setPhone(rs.getString("phone"));
				contact.setTitle(rs.getString("title"));
				contact.setQuestion(rs.getString("question"));
				contact.setDateCreate(rs.getString("dateCreate"));
				contact.setIsRead(rs.getInt("isRead"));
				list.add(contact);
			}
			rs.close();
			preSta.close();
			ConnectDao.close();
		} catch (Exception e) {
			e.printStackTrace();
			return list;
		}
		return list;
	}

	public String saveContac(Contact contact) {
		try {
			PreparedStatement preSta = connection
					.prepareStatement(SAVE_CONTACT);

			preSta.setString(1, contact.getFullName());
			preSta.setString(2, contact.getEmail());
			preSta.setString(3, contact.getPhone());
			preSta.setString(4, contact.getTitle());
			preSta.setString(5, contact.getQuestion());
			preSta.setString(6, contact.getDateCreate());
			preSta.setInt(7, contact.getIsRead());

			preSta.executeUpdate();
			preSta.close();
			ConnectDao.close();
		} catch (Exception e) {
			e.printStackTrace();
			return Contants.ERROR;
		}
		return Contants.SUCCESS;
	}

	@Override
	public List<Contact> getContactByID(long contactID) {
		List<Contact> list = new ArrayList<Contact>();
		try {
			updateStatusContact(contactID);
			PreparedStatement preSta = connection
					.prepareStatement(GET_CONTACT_BYID);
			preSta.setLong(1, contactID);
			ResultSet rs = preSta.executeQuery();

			while (rs.next()) {
				Contact contact = new Contact();
				contact.setContactID(rs.getLong("id"));
				contact.setFullName(rs.getString("fullName"));
				contact.setEmail(rs.getString("email"));
				contact.setPhone(rs.getString("phone"));
				contact.setTitle(rs.getString("title"));
				contact.setQuestion(rs.getString("question"));
				contact.setDateCreate(rs.getString("dateCreate"));
				contact.setIsRead(rs.getInt("isRead"));
				list.add(contact);
			}
			rs.close();
			preSta.close();
		} catch (Exception e) {
			e.printStackTrace();
			return list;
		}finally{
			if(connection!=null){
				try {
					ConnectDao.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	
	private void updateStatusContact(long contactID){
		try {
			PreparedStatement preSta = connection
					.prepareStatement(UPDATE_CONTACT_ISREAD);
			preSta.setLong(1, contactID);
			preSta.executeUpdate();
			preSta.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
